﻿
using System;
namespace GNUGo.Net.CLI.CliControls {
    public class CliGraphics {
        private readonly int _width;
        private readonly int _height;
        private readonly char[,] _charData;
        private readonly ConsoleColor[,] _foregroundData;
        private readonly ConsoleColor[,] _backgroundData;

        public CliGraphics(int width, int height, ConsoleColor forecolor, ConsoleColor backcolor) {
            _width = width;
            _height = height;
            _charData = new char[width, height];
            _foregroundData = new ConsoleColor[width, height];
            _backgroundData = new ConsoleColor[width, height];
            for (int y = 0; y < _height; y++) {
                for (int x = 0; x < _width; x++) {
                    _charData[x, y] = ' ';
                    _foregroundData[x, y] = forecolor;
                    _backgroundData[x, y] = backcolor;
                }
            }
        }

        public int Width {
            get { return _width; }
        }

        public int Height {
            get { return _height; }
        }

        internal void DrawToScreen(int left, int top) {
            int prevCursorX = Console.CursorLeft;
            int prevCursorY = Console.CursorTop;

            for (int y = 0; y < _height; y++) {
                Console.CursorLeft = left;
                Console.CursorTop = top + y;
                for (int x = 0; x < _width; x++) {
                    Console.ForegroundColor = _foregroundData[x, y];
                    Console.BackgroundColor = _backgroundData[x, y];
                    Console.Write(_charData[x, y]);
                }
            }

            Console.CursorLeft = prevCursorX;
            Console.CursorTop = prevCursorY;
        }

        public void DrawString(string text, ConsoleColor foreground, ConsoleColor background, int x, int y) {
            if (text == null) {
                return;
            }
            string[] textLines = text.Split('\n');
            for (int line = 0; line < textLines.Length; line++) {
                string textLine = textLines[line];
                for (int u = 0; u < textLine.Length; u++) {
                    DrawCharacter(textLine[u], foreground, background, x + u, y + line);
                }
            }
        }

        public void DrawCharacter(char c, ConsoleColor foreground, ConsoleColor background, int x, int y) {
            if (x < 0 || x >= _width || y < 0 || y >= _height) {
                return;
            }
            _charData[x, y] = c;
            _foregroundData[x, y] = foreground;
            _backgroundData[x, y] = background;
        }

        public void DrawGraphics(CliGraphics g, int left, int top) {
            for (int y = 0; y < g._height; y++) {
                for (int x = 0; x < g._width; x++) {
                    _charData[x + left, y + top] = g._charData[x, y];
                    _foregroundData[x + left, y + top] = g._foregroundData[x, y];
                    _backgroundData[x + left, y + top] = g._backgroundData[x, y];
                }
            }
        }
    }
}
